// pages/tickets_detail/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{ getOrder,orderSubmitFn} from '../../api/index'
import {formatTime} from '../../utils/util'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {baseURL} from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:1,
    list:[],
    priceMax:0,
    couponsShow:false,
    siteShow:false,

    shop_addr:false,
    count:1,
    ticketsList:{},
    ticketsId:0,
    ticketsPrice:0,
    ticketsAll:0,
    flag:true
  },

  onChange(event) {
    this.setData({
      count:event.detail
    })
    this.ticketsFn()
  },
  // 商品支付
  shopSubmit(){
    var that =this
    if(this.data.flag){
      this.setData({
        flag:false
      })
      var arr = []
      this.data.list.forEach(item=>{
        arr.push({
          id:item.goods_id,
          num:item.goods_count
        })
      })
      var obj = {
        uid:wx.getStorageSync('uid'),
        type:'goods',
        goods:arr,
        name:this.data.shop_addr.name || '',
        mobile:this.data.shop_addr.mobile || '',
        address: this.data.shop_addr.addr + this.data.shop_addr.address || ''
      }
      orderSubmitFn(obj).then(res=>{
        if(res.sta == 1){
          wx.requestPayment({
            timeStamp:res.wxParams.timeStamp,
            nonceStr:res.wxParams.nonceStr,
            package:"prepay_id="+res.wxParams.prepay_id,
            signType:'MD5',
            paySign:res.wxParams.paySign,
            success(obj){
              that.setData({
                flag:true
              })
              var arr = that.data.list
              var cart = wx.getStorageSync('cart') || []
              arr.forEach(item=>{
                var Index = cart.findIndex((item1,i)=>{
                    return item.goods_id == item1.goods_id
                })
                cart.splice(Index,1);
              })
              wx.setStorageSync('cart', cart)
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function(){
                wx.navigateBack()
              },1000)
            },
            fail(res){
              that.setData({
                flag:true
              })
              wx.showToast({
                title: '支付失败',
              })
            }
          })
        }else{
          wx.showToast({
            icon:'error',
            title: typeof res.msg == 'string' ? res.msg : '发生错误',
          })
        }
      })
    }

  },
  // 门票支付
  ticketsFnSubmit(){
    var that = this
    if(this.data.flag){
      this.setData({
        flag:false
      })
      var arr = []
      arr.push({
        id:this.data.ticketsList.ticket.id,
        num:this.data.count
      })
      var obj = {
        uid:wx.getStorageSync('uid'),
        type:'ticket',
        card_id:this.data.ticketsList.card ? this.data.ticketsList.card.id : 0,
        goods:arr
      }
      orderSubmitFn(obj).then(res=>{
        if(res.sta == 1){
          wx.requestPayment({
            timeStamp:res.wxParams.timeStamp,
            nonceStr:res.wxParams.nonceStr,
            package:"prepay_id="+res.wxParams.prepay_id,
            signType:'MD5',
            paySign:res.wxParams.paySign,
            success(obj){
              that.setData({
                flag:true
              })
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function(){
                wx.navigateBack()
              },1000)
            },
            fail(res){
              that.setData({
                flag:true
              })
              wx.showToast({
                title: '支付失败',
              })
            }
          })
        }else{
          wx.showToast({
            icon:'error',
            title: typeof res.msg == 'string' ? res.msg : '发生错误',
          })
        }
      })
    }

  },
  // 总额
  priceAllFn(){
    var price = this.data.ticketsList.ticket.price
    var count = this.data.count

    var num = Number(price) * Number(count)
    this.setData({
      ticketsAll:num.toFixed(2)
    })
  },

  // 实付金额
  priceFn(){
    var price = this.data.ticketsList.ticket.price
    var count = this.data.count
    var jian = this.data.ticketsList.card ? this.data.ticketsList.card.jian : 0

    var num = Number(price) * Number(count) - Number(jian)
    this.setData({
      ticketsPrice:num.toFixed(2)
    })
  },

  adrrFn(){
    var obj ={
      uid:wx.getStorageSync('uid'),
      type:'goods',
      // address_id:'',
    }
    getOrder(obj).then(res=>{
      if(res.sta == 1){
        this.setData({
          shop_addr:res.addr
        })
      }else if(res.sta == 0){
        showText('请先填写地址！',2)
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/me_site/index?status=' + 1,
          })
        }, 1000);
      }else{
        showText('发送错误！',2)
      }
    })
  },

  // 门票
  ticketsFn(){
    var obj ={
      uid:wx.getStorageSync('uid'),
      type:'ticket',
      ticket_id:this.data.ticketsId,
      num:this.data.count
    }
    getOrder(obj).then(res=>{
      if(res.sta == 1){
        this.setData({
          ticketsList:res
        })
        this.priceAllFn()
        this.priceFn()
      }else{
        showText('发送错误！',2)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      if(options.id == 1){
        var arr = wx.getStorageSync('goods_lj')
        this.setData({
          list:wx.getStorageSync('goods_order') || arr || [],
          priceMax:wx.getStorageSync('priceMax') || arr[0].goods_price,
        })
        wx.removeStorageSync('goods_order')
        wx.removeStorageSync('priceMax')
        wx.removeStorageSync('goods_lj')
        // this.adrrFn()
      }else if(options.id == 2){
        this.setData({
          ticketsId:options.status
        })
        this.ticketsFn()
      }
      
      this.setData({
        status:options.id
      })
    }
  },

  nav(e){
    navTo(e)
  },
  click(e){
    onClick(e,this)
  },
  change(e){
    onInput(e,this)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.status == 1){
      this.adrrFn()
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})